package com.woniuxy.three.pms.roomtype.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.three.pms.bean.RoomTypeBean;
import com.woniuxy.three.pms.dao.RoomBeanMapper;
import com.woniuxy.three.pms.dao.RoomTypeBeanMapper;
import com.woniuxy.three.pms.roomtype.service.IRoomTypeService;


/**
* @ProjectName: PMS
* @Package: com.woniuxy.three.pms.room.service.impl.RoomTypeServiceImpl
* @ClassName: IRoomService 
* @Author:  subai-dhl
* @Description: 房间类型管理模块 
* @Date: 2019/6/20 11:46 
* @Version: 1.0 版本
* @See：参考版本
*/
@Service
public class RoomTypeServiceImpl implements IRoomTypeService {

	@Autowired
	private RoomTypeBeanMapper RTdao;
	
	@Autowired
	private RoomBeanMapper Rdao;
	/**
	 * @Method findAllType
	 * @Author subai-dhl
	 * @Description 查询该酒店的所有类型房间
	 * @Version 1.0
	 * @return
	 * @Date 2019/6/19 17:57
	 */
	@Override
	public List<RoomTypeBean> findAllType() {
		List<RoomTypeBean> list = RTdao.selectAllType();
		System.out.println(list.size());
		for (RoomTypeBean roomTypeBean : list) {
			String imgs = roomTypeBean.getImgs();
			String images[] = imgs.split(";");
			roomTypeBean.setImages(images);
		}
		
		return list;
	}

	/**
	 * @Method addRoomType
	 * @Author subai-dhl
	 * @Description 新增房间类型
	 * @Version 1.0
	 * @param roomT RoomTypeBean
	 * @paramask 除id外不允许为null
	 * @return
	 * @Date 2019/6/19 18:49
	 */
	@Override
	public boolean addRoomType(RoomTypeBean roomT) {
		int number = 0;
		if(roomT!=null) {
			number = RTdao.insert(roomT);
		}
		if(number>0) {
			return true;
		}
		return false;
	}

	/**
	 * @Method changeRoomType
	 * @Author subai-dhl
	 * @Description 修改房间类型，包括优惠政策、房间库存
	 * @Version 1.0
	 * @param roomT
	 * @return
	 * @Date 2019/6/19 18:58
	 */
	@Override
	public boolean changeRoomType(RoomTypeBean roomT) {
		if(roomT.getId()==null) {
			return false;
		}
		int number = RTdao.updateByPrimaryKeySelective(roomT);
		if(number>0) {
			return true;
		}
		return false;
	}

	
	/**
	 * @Method deleteRoomType
	 * @Author subai-dhl
	 * @Description 删除房间类型
	 * @Version 1.0
	 * @param RoomTypeId
	 * @return
	 * @Date 2019/6/19 18:58
	 */
	@Override
	public boolean deleteRoomType(Integer RoomTypeId) {
		int number = 0;
		if(RoomTypeId!=null) {
			number = RTdao.deleteByPrimaryKey(RoomTypeId);
		}
		if(number>0) {
			return true;
		}
		return false;
	}

	/**
	 * @Method findById
	 * @Author subai-dhl
	 * @Description 通过id查询该类房间详细信息
	 * @Version 1.0
	 * @return
	 * @Date 2019/6/19 18:58
	 */
	@Override
	public RoomTypeBean findById(Integer roomTypeID) {
		RoomTypeBean roomT=null;
		if(roomTypeID!=null) {
			roomT = RTdao.selectByPrimaryKey(roomTypeID);
		}
		String imgs = roomT.getImgs();
		String images[] = imgs.split(";");
		roomT.setImages(images);
		return roomT;
	}
	
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)
	@Override
	public boolean changeStock() {
		List<RoomTypeBean> list = RTdao.selectAllType();
		for (RoomTypeBean roomTypeBean : list) {
			int number = Rdao.findRoomNumberByRoomType(roomTypeBean.getId());
			RoomTypeBean newRoom = new RoomTypeBean();
			newRoom.setStock1(roomTypeBean.getStock2());
			newRoom.setStock2(roomTypeBean.getStock3());
			newRoom.setStock3(roomTypeBean.getStock4());
			newRoom.setStock4(roomTypeBean.getStock5());
			newRoom.setStock5(roomTypeBean.getStock6());
			newRoom.setStock6(roomTypeBean.getStock7());
			newRoom.setStock7(number);
			newRoom.setId(roomTypeBean.getId());
			int number1 = RTdao.updateByPrimaryKeySelective(newRoom);
			if(number1<=0) {
				return false;
			}
		}
		return true;
	}

	
	@Override
	public List<RoomTypeBean> findAllType1() {
		
		return RTdao.selectAllType1();
	}

	@Override
	public int findStock(Integer roomtId) {
		return Rdao.findRoomNumberByRoomType(roomtId);
	}

}
